home *** CD-ROM | disk | FTP | other *** search
- From: maven@mavenry.altcit.eskimo.com (Norman Hamer)
- Message-ID: <646916@mavenry.altcit.eskimo.com>
- Path: news.sprintlink.net!eskimo!mavenry
- Newsgroups: comp.std.c
- Subject: Safe Multiply, std ok.
- X-AltCit-ID: 646916
- Date: Thu, 11 Jan 96 15:04:37 GMT
-
-
- Daniel- Sure there's a way. It's slow and evil (at least in this
- incarnation), but it makes a lot more sense for floats...
-
- #include <math.h>
- #include <limits.h>
-
- int bad_mult(int num1, int num2)
- {
- return (log(abs(num1)) * log(abs(num2)) > log(INT_MAX)) ? 1 : 0;
- }
-
- ... now, this'll give one false negative (if the two numbers multiply to
- produce INT_MIN on a 2's complement machine or something where INT_MIN isn't
- real close in magnitude to INT_MAX), but that could be taken care of by
- slight additional code to check signs and compare against log(abs(INT_MIN))
- if necessary.
-
- On the other hand, this is really going to slow down simple integer
- multiplies. ;)
-